HackerRank Super Reduced String
提出
11/16
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'superReducedString' function below.
#
# The function is expected to return a STRING.
# The function accepts STRING s as parameter.
#
def reduce_s(s):
ans = []
i = 0
while (i <= len(s) - 1):
if (i == len(s) - 1):
break
i += 2
else:
i += 1
if (len(s) == len(set(s))):
return "".join(ans)
else:
return reduce_s("".join(ans))
def superReducedString(s):
# Write your code here
# maximum recursion depth exceeded while calling a Python object
ans = reduce_s(s)
if (len(ans)):
return "".join(ans)
else:
return "Empty String"
if __name__ == '__main__':
s = input()
result = superReducedString(s)
fptr.write(result + '\n')
fptr.close()
# input
# zztqooauhujtmxnsbzpykwlvpfyqijvdhuhiroodmuxiobyvwwxupqwydkpeebxmfvxhgicuzdealkgxlfmjiucasokrdznmtlwh
# expected
# tqauhujtmxnsbzpykwlvpfyqijvdhuhirdmuxiobyvxupqwydkpbxmfvxhgicuzdealkgxlfmjiucasokrdznmtlwh
# result
解説
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'superReducedString' function below.
#
# The function is expected to return a STRING.
# The function accepts STRING s as parameter.
#
def superReducedString(s):
# Write your code here
result = []
# baab
# b -> ba -> b -> []
for i in range(len(s)):
if len(result) == 0 or result-1 != si: else:
result.pop()
if (len(result) == 0):
return "Empty String"
else:
return "".join(result)
if __name__ == '__main__':
s = input()
result = superReducedString(s)
fptr.write(result + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=sKlGc3ySe9c
提出
code: python
import math
import os
import random
import re
import sys
from collections import deque
#
# Complete the 'superReducedString' function below.
#
# The function is expected to return a STRING.
# The function accepts STRING s as parameter.
#
def superReducedString(s):
# Write your code here
# aaabccddd
# baab
q = deque()
for i in range(1, len(s)):
if len(q) == 0:
else:
q.pop()
else:
if len(q) == 0:
return "Empty String"
else:
return "".join(q)
if __name__ == '__main__':
s = input()
result = superReducedString(s)
fptr.write(result + '\n')
fptr.close()